﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using HiauPdm.Bll;
using HiauPdm.Web;

namespace HiauPdm.Web
{
    public partial class WzCk : PageBase
    {
        /// <summary>
        /// 加载
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.MaintainScrollPositionOnPostBack = true;
            if (Page.IsPostBack) { return; }
            BtnCk.OnClientClick = "if(GridCz('ckqr','')){this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(BtnCk, null) + ";}return false;";
            BindData(Request.QueryString["sWhere"]);
            TextWhere.Focus();
        }

        /// <summary>
        /// 绑定刷新数据
        /// </summary>
        private void BindData(string sWhere)
        {
            sWhere = (sWhere == null ? string.Empty : sWhere.Trim());
            // 查询数据
            DataSet ds = BCkGl.GetWzCkByCx(sWhere);
            if (ds == null || ds.Tables[0].Rows.Count <= 0)
            {
                PanelList1.Visible = true;
                PanelList2.Visible = false;
                return;
            }
            PanelList1.Visible = false;
            PanelList2.Visible = true;
            // 绑定表格
            GridData.DataSource = ds;
            GridData.DataBind();
            GridData.SelectedIndex = -1;
        }

        /// <summary>
        /// 创建行时
        /// </summary>
        protected void GridData_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                CheckBox oCheckBox = new CheckBox();
                oCheckBox.ToolTip = "勾选全部/取消勾选全部";
                oCheckBox.Attributes["onclick"] = "SelectCheckAll(this);";
                e.Row.Cells[0].Controls.Add(oCheckBox);
            }
        }

        /// <summary>
        /// 操作[编辑]
        /// </summary>
        protected void GridData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "bj")
            {
                int iSelectId = Convert.ToInt32(GridData.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                BindData(Request.QueryString["sWhere"]);
                for (int idx = 0; idx < GridData.DataKeys.Count; idx++)
                {
                    if ((int)GridData.DataKeys[idx].Value == iSelectId)
                    {
                        GridData.SelectedIndex = idx;
                        break;
                    }
                }
            }
        }

        /// <summary>
        /// 出库
        /// </summary>
        protected void BtnCk_Click(object sender, EventArgs e)
        {
            string sCkWzs = string.Empty;
            for (int i = 0; i < GridData.Rows.Count; i++)
            {
                if (((CheckBox)GridData.Rows[i].Cells[0].Controls[1]).Checked)
                {
                    sCkWzs += string.Format("{0},", GridData.DataKeys[i].Value);
                }
            }
            if (sCkWzs.Length > 0)
            {
                sCkWzs = sCkWzs.Substring(0, sCkWzs.Length - 1);
            }
            string sWzCkBh = string.Empty;
            if (!ShowErr(BCkGl.ExeWzCk(sCkWzs, (string)Session["RealName"], ref sWzCkBh)))
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "ShowPrint", "window.attachEvent('onload',function(){GridCz('dy','" + sWzCkBh + "');self.location.replace(self.location);});", true);
            }
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "OnLoadSelColor", "window.attachEvent('onload',function(){SelColor(document.getElementById('" + GridData.ClientID + "'));});", true);
        }
    }
}